﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.SessionState;
using SES.CMS.DO;
using SES.CMS.BL;

namespace SES.CMS
{
    public partial class Default2 : System.Web.UI.Page
    {
        private static ClientLoginDO obj = new ClientLoginDO();

        protected void Page_Load(object sender, EventArgs e)
        {
            lblCopyright.Text = GetFooterInfo();
            if (!IsPostBack)
            {
                testtk();
                HttpSessionState ss = HttpContext.Current.Session;
                Session["SessionID"] = ss.SessionID;
                if (Session["TenDN"] == null)
                {
                    pbLogin.Visible = true;
                    pnLogout.Visible = false;

                }
                else
                {
                    pbLogin.Visible = false;
                    pnLogout.Visible = true;
                    txtDN.Text = "Xin chào: <a href='/ThongTin/" + obj.ClientLoginID + "/Thong-tin.aspx'><span style=\"color:red;font-size:13px;\"> " + Session["TenDN"].ToString() + "</span></a>";
                }
            }
            lblSupportOnline.Text = new sysConfigBL().Select(new sysConfigDO { ConfigID = 3 }).ConfigValue;
        }

        protected void txtTK_TextChanged(object sender, EventArgs e)
        {
            Response.Redirect("Search/"+txtTK.Text.Trim()+"/Tim-Kiem.aspx");
        }

        private void txtTK_Enter(object sender, System.EventArgs e)
        {
            txtTK_TextChanged((object)sender, (EventArgs)e);
        }

        public void _btnLogin_Click(object sender, EventArgs e)
        {
            string txtPass = Ultility.EncryptMd5(_txtPassword.Text);
            obj = new ClientLoginBL().SelectLogin(_txtUserid.Text, txtPass);
            if (obj.ClientLoginID > 0)
            {
                testcookie();
                Session["TenDN"] = _txtUserid.Text;
                Session["IDDN"] = obj.ClientLoginID.ToString();
                Ultility.Alert("Bạn đã đăng nhập thành công!!!","/Default.aspx");
            }
            else
            {
            }
        }

        protected void lbThoat_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Response.Redirect("/Default.aspx");
        }
        private void testcookie()
        {
            if (Request.Browser.Cookies == true)
            {
                if (chkNhomk.Checked == true)
                {
                    HttpCookie ckaccount = new HttpCookie("account");
                    ckaccount["TenDN"] = _txtUserid.Text;
                    ckaccount["Pass"] = _txtPassword.Text;
                    ckaccount.Expires.AddMonths(1);
                    Response.Cookies.Add(ckaccount);
                }
            }
        }
        private void testtk()
        {
            if (Request.Cookies["account"] != null | Convert.ToString(Request.Cookies["account"]) != "")
            {
                HttpCookie ckaccount = Request.Cookies["account"];
                _txtUserid.Text = ckaccount["TenDN"].ToString();
                _txtPassword.Text = ckaccount["Pass"].ToString();
                if (ckaccount["TenDN"] == "")
                {
                    chkNhomk.Checked = false;
                }
                else
                    chkNhomk.Checked = true;
            }
        }
        private string GetFooterInfo()
        {
            sysConfigDO objConfig = new sysConfigBL().Select(new sysConfigDO { ConfigID = 11 });
            return objConfig.ConfigValue.ToString();
        }
    }
}
